#!/bin/bash

OK=0
WARN=1
CRITICAL=2
PATH="/bin:/sbin:/usr/bin:/usr/sbin"



if [ `ps -C rpcbind | wc -l` -lt 2 ]; then
    filename=`date +'%Y%m%d'`
    echo "PORTMAP CRITICAL: Rpcbind is NOT running!" >> /home/jiangxw/multi-Daemon-log/$filename
    echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/jiangxw/multi-Daemon-log/$filename
    echo "PORTMAP CRITICAL: Rpcbind is NOT running!"
    checkconfigure_rpc=`chkconfig --list rpcbind | grep ":on"`
    checkconfigure_nfs=`chkconfig --list nfs | grep ":on"`
    if [[ "$checkconfigure_rpc"  && "$checkconfigure_nfs" ]]; then
        /etc/init.d/rpcbind restart
        /etc/init.d/nfs restart
        exit $CRITICAL
    fi
else
    if [ `ps -C nfsd | wc -l` -lt 2 ]; then
        filename=`date +'%Y%m%d'`
        echo "NFS CRITICAL: Nfs is NOT running!" >> /home/jiangxw/multi-Daemon-log/$filename
        echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/jiangxw/multi-Daemon-log/$filename
        echo "NFS CRITICAL: Nfs is NOT running!"
        checkconfigure_nfs=`chkconfig --list nfs |grep ":on"`
        if [ "$checkconfigure_nfs" ]; then
            /etc/init.d/nfs restart
            exit $CRITICAL
        fi
    else
        if [ `netstat -a | grep -E "tcp.*:nfs" |wc -l` -lt 1 ]; then
            filename=`date +'%Y%m%d'`
            echo "NFS CRITICAL: Host is NOT listening on TCP port 2049!" >> /home/jiangxw/multi-Daemon-log/$filename
            echo HOSTNAME: `hostname`, TIME: `date +'%H:%m:%s'` >> /home/jiangxw/multi-Daemon-log/$filename
            /etc/init.d/nfs restart
            echo "NFS CRITICAL: Host is NOT listening on TCP port 2049!"
            exit $CRITICAL
        fi
    fi
fi
if [ `netstat -aetp |grep -E "tcp.*:nfs" |wc -l` -gt 500 ]; then
    filename=`date +'%Y%m%d'`
    echo "NFS CRITICAL: Nfs connection is too busy!" >> /home/jiangxw/multi-Daemon-log/$filename
    echo HOSTNAME: `hostname`, TIME: `date +'%H:%m:%s'` >> /home/jiangxw/multi-Daemon-log/$filename
    #echo "NFS CRITICAL: Nfs connection is too busy!"
    netstat -aetp | awk -F' ' '{print $7}' | sort | uniq -c | awk -F' ' '{print $1,$2}' | sort -k1frn >> /home/jiangxw/multi-Daemon-log/netstat
    count=($(cut -d ' ' -f1 /home/jiangxw/multi-Daemon-log/netstat))
    user=($(cut -d ' ' -f2 /home/jiangxw/multi-Daemon-log/netstat))
    cat /dev/null > /home/jiangxw/multi-Daemon-log/netstat
    total=("USER:")
    for ((i=0; i<${#count[@]}; i++)); do
        if [[ ${count[i]} > 100 ]]; then
            total="${total}""  ""${user[i]}"
            #echo "NFS CRITICAL: the user ${user[i]} is CRITICAL!" >> /home/jiangxw/multi-Daemon-log/netstat
            #echo "NFS CRITICAL: the user ${user[i]} is CRITICAL!"
        fi
    done
    echo "NFS CRITICAL: ${total} is CRITICAL!" >> /home/jiangxw/multi-Daemon-log/$filename
    echo "NFS CRITICAL: ${total} is CRITICAL!"
    exit $CRITICAL
fi

if [ `netstat -aetp |grep -E "tcp.*:nfs" |wc -l` -gt 300 ]; then
    filename=`date +'%Y%m%d'`
    echo "NFS WARNNING: Nfs connection is too much!" >> /home/jiangxw/multi-Daemon-log/$filename
    echo HOSTNAMW: `hostname`, TIME: `date +'%H:%m:%s'` >> /home/jiangxw/multi-Daemon-log/$filename
    #echo "NFS WARNING: Nfs connection is too much!"
    netstat -aetp | awk -F' ' '{print $7}' | sort | uniq -c | awk -F' ' '{print $1,$2}' | sort -k1frn >> /home/jiangxw/multi-Daemon-log/netstat
    count=($(cut -d ' ' -f1 /home/jiangxw/multi-Daemon-log/netstat))
    user=($(cut -d ' ' -f2 /home/jiangxw/multi-Daemon-log/netstat))
    cat /dev/null > /home/jiangxw/multi-Daemon-log/netstat
    total="USER:"
    echo ${user[0]}
    for ((i=0; i<${#count[@]}; i++)); do
        if [[ ${count[i]} > 100 ]]; then
            total="${total}""  ""${user[i]}"   
            #echo "NFS WARNING: the user ${user[i]} is ocupying TOO MANY connections!" >> /home/jiangxw/multi-Daemon-log/netstat
            #echo "NFS WQRNING: the user ${user[i]} is ocupying TOO MANY connections!"
        fi
    done
    echo "NFS WARNNING: ${total} is ocupying TOO MANY connections!" >> /home/jiangxw/multi-Daemon-log/$filename
    echo "NFS WARNNING: ${total} is ocupying TOO MANY connections!"
    exit $WARN
fi


echo "NFS OK: Daemon is running. Host is listening."
exit $OK




        

